package com.example.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.example.mybatis.domain.Shop;
import com.example.mybatis.mapper.ShopMapper;
import com.example.service.IShopService;
import com.example.vo.AmapResult;
import com.example.vo.Geocode;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.toList;

/**
 * 商店 服务层实现
 * 
 * @author ruoyi
 * @date 2020-06-16
 */
@Service
public class ShopServiceImpl implements IShopService
{
	@Autowired
	private ShopMapper shopMapper;
	@Autowired
	@Qualifier("threadPoolTaskExecutor")
	private ThreadPoolTaskExecutor executor;

	@Override
	public void getShopGeoBatch() {

		PageHelper.startPage(0, 500, "id");
		Shop para = new Shop();
		para.setStatus("0");
		List<Shop> shops = shopMapper.selectShopList(para);

		List<CompletableFuture<Shop>> res = shops.stream()
				.map(s -> CompletableFuture.supplyAsync(() -> invokeAmapApi(s), executor))
				.collect(toList());

		List<Shop> shopList = res.stream()
				.map(CompletableFuture::join)
				.filter(s -> "1".equals(s.getStatus())).collect(toList());

		if (CollectionUtil.isNotEmpty(shopList)) {
			shopMapper.updateShopBatch(shopList);
		}
	}

	private Shop invokeAmapApi(Shop shop) {

		String url = "https://restapi.amap.com/v3/geocode/geo";
		Map<String, Object> param = new HashMap<>();
		param.put("key", "3380c71d844f388d74639ae48b1289c7");
		param.put("address", shop.getProvince() + shop.getCity() + shop.getAddress());

		String res = HttpUtil.get(url, param);
		AmapResult result = JSON.parseObject(res, AmapResult.class);

		if (result.getCount() > 0) {
			Geocode geocode = result.getGeocodes().get(0);
			shop.setStatus("1");
			shop.setAdcode(geocode.getAdcode());
			shop.setLocation(geocode.getLocation());
			shop.setUpdateTime(new Date());
		}

		return shop;
	}

	/**
     * 查询商店信息
     * 
     * @param id 商店ID
     * @return 商店信息
     */
    @Override
	public Shop selectShopById(Integer id)
	{
	    return shopMapper.selectShopById(id);
	}
	
	/**
     * 查询商店列表
     * 
     * @param shop 商店信息
     * @return 商店集合
     */
	@Override
	public List<Shop> selectShopList(Shop shop)
	{
	    return shopMapper.selectShopList(shop);
	}
	
    /**
     * 新增商店
     * 
     * @param shop 商店信息
     * @return 结果
     */
	@Override
	public int insertShop(Shop shop)
	{
	    return shopMapper.insertShop(shop);
	}
	
	/**
     * 修改商店
     * 
     * @param shop 商店信息
     * @return 结果
     */
	@Override
	public int updateShop(Shop shop)
	{
	    return shopMapper.updateShop(shop);
	}

	/**
     * 删除商店对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteShopByIds(String ids)
	{
		return shopMapper.deleteShopByIds(Convert.toStrArray(ids));
	}
	
}
